// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema

// import { validator } from "./opendb-news-articles";

const validator = {
  user_id: {
    rules: [
      {
        format: "string",
      },
    ],
  },
  company_name: {
    rules: [
      {
        required: true,
      },
      {
        format: "string",
      },
      {
        maxLength: 100,
      },
    ],
    title: "企业名称",
    label: "企业名称",
  },
  business_license: {
    rules: [
      {
        required: true,
      },
      {
        format: "file",
      },
    ],
    title: "营业执照",
    label: "营业执照",
  },
  legal_person_idcard_national: {
    rules: [
      {
        required: true,
      },
      {
        format: "file",
      },
    ],
    title: "法人身份证（国徽面）",
    label: "法人身份证（国徽面）",
  },
  legal_person_idcard_portrait: {
    rules: [
      {
        required: true,
      },
      {
        format: "file",
      },
    ],
    title: "法人身份证（头像面）",
    label: "法人身份证（头像面）",
  },
  auth_status: {
    rules: [
      {
        format: "string",
      },
      {
        range: [
          {
            value: "pending",
            text: "pending",
          },
          {
            value: "approved",
            text: "approved",
          },
          {
            value: "rejected",
            text: "rejected",
          },
        ],
      },
    ],
    title: "认证状态",
    label: "认证状态",
  },
  submit_time: {
    rules: [
      {
        format: "timestamp",
      },
    ],
    title: "提交时间",
    defaultValue: {
      $env: "now",
    },
    label: "提交时间",
  },
  audit_time: {
    rules: [
      {
        format: "timestamp",
      },
    ],
    title: "审核时间",
    label: "审核时间",
  },
  audit_remark: {
    rules: [
      {
        format: "string",
      },
      {
        maxLength: 500,
      },
    ],
    title: "审核备注",
    label: "审核备注",
  },
  contact_info: {
    rules: [
      {
        required: true,
      },
      { format: "number" },
      {
        trigger: ["change", "blur"],
        pattern: /^1[3-9]\d{9}$/,
        errorMessage: "请输入正确的手机号",
      },
    ],
    title: "手机号码",
    label: "手机号码",
  },
};

const enumConverter = {
  auth_status_valuetotext: {
    pending: "pending",
    approved: "approved",
    rejected: "rejected",
  },
};

function filterToWhere(filter, command) {
  let where = {};
  for (let field in filter) {
    let { type, value } = filter[field];
    switch (type) {
      case "search":
        if (typeof value === "string" && value.length) {
          where[field] = new RegExp(value);
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = [];
          for (let s of value) {
            selectValue.push(command.eq(s));
          }
          where[field] = command.or(selectValue);
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0];
          let lt = value[1];
          where[field] = command.and([command.gte(gt), command.lte(lt)]);
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value;
          let startDate = new Date(s);
          let endDate = new Date(e);
          where[field] = command.and([
            command.gte(startDate),
            command.lte(endDate),
          ]);
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value;
          where[field] = command.and([
            command.gte(startDate),
            command.lte(endDate),
          ]);
        }
        break;
    }
  }
  return where;
}

export { validator, enumConverter, filterToWhere };
